package org.graduates.webapp.actions.course;

import java.util.Date;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.graduates.webapp.actions.BaseAction;
import org.projects.graduates.domain.Course;
import org.projects.graduates.domain.Repeat;
import org.projects.graduates.domain.Teaching;
import org.projects.graduates.domain.TimeTable;

import com.opensymphony.xwork2.ActionSupport;

@Action(results = { @Result(name = "success", type = "redirect", location = "teachings.action", params = { "tId", "${courseId}" }) })
public class CommitArrangement extends BaseAction {

	private static final long serialVersionUID = -1910774533713853302L;

	private long courseId = 0L;

	private Repeat repeat;

	public String execute() {
		Course course = Course.get(Course.class, courseId);
		if (null == repeat || null == course) {
			return ActionSupport.INPUT;
		}
		
		gradApplication.save(repeat);
		
		Teaching tc = Teaching.findBy(course);
		tc.setRepeat(repeat);
		gradApplication.save(tc);
		
		for (Date date : repeat.arrangement()) {
			TimeTable tt = new TimeTable();
			tt.setTeachDate(date);
			tt.setCourse(course);
			gradApplication.save(tt);
		}
		
		return ActionSupport.SUCCESS;
	}

	public long getCourseId() {
		return courseId;
	}

	public void setCourseId(long courseId) {
		this.courseId = courseId;
	}

	public Repeat getRepeat() {
		return repeat;
	}

	public void setRepeat(Repeat repeat) {
		this.repeat = repeat;
	}

}
